package com.lmy.mallback.controller.admin;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lmy.mallback.constant.SystemConstant;
import com.lmy.mallback.entity.Admin;
import com.lmy.mallback.service.AdminService;
import com.lmy.mallback.util.JwtUtils;
import com.lmy.mallback.util.PageBean;
import com.lmy.mallback.util.R;
import com.lmy.mallback.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @author 程序员小洋
 * @version 2022.3
 * @date 2023/1/18 13:13
 * @title 管理员
 */
@SuppressWarnings("all")
@RestController
public class AdminController {

    @Autowired
    private AdminService adminService;

    /**
     * 管理员登录
     * @param admin
     * @return
     */
    @PostMapping("/adminLogin")
    public R adminLogin(@RequestBody Admin admin){
        if(admin==null){
            return R.error();
        }
        if(StringUtil.isEmpty(admin.getUserName())){
            return R.error("用户名不能为空！");
        }
        if(StringUtil.isEmpty(admin.getPassword())){
            return R.error("密码不能为空！");
        }
        Admin resultAdmin = adminService.getOne(new QueryWrapper<Admin>().eq("userName", admin.getUserName()).or().eq("email",admin.getEmail()));
        if(resultAdmin==null){
            return R.error("用户名或邮箱不存在！");
        }
        if(!resultAdmin.getPassword().trim().equals(admin.getPassword())){
            return R.error("用户名或者密码错误！");
        }
        String token = JwtUtils.createJWT("-1", "admin", SystemConstant.JWT_TTL);
        Map<String,Object> resultMap=new HashMap<String,Object>();
        resultMap.put("token",token);
        return R.ok(resultMap);
    }


    /**
     * 修改密码
     * @param admin
     * @return
     */
    @PostMapping("/admin/modifyPassword")
    public R modifyPassword(@RequestBody Admin admin){
        if(StringUtil.isEmpty(admin.getUserName())){
            return R.error("用户名不能为空！");
        }
        if(StringUtil.isEmpty(admin.getPassword())){
            return R.error("密码不能为空！");
        }
        adminService.update(new UpdateWrapper<Admin>().set("password",admin.getNewPassword())
                .eq("userName",admin.getUserName()));
        return R.ok();
    }

    /**
     * 用户注册
     */
    @PostMapping("/adminRegister")
    public R adminRegister(@RequestBody Admin admin) {
        Admin resultAdmin = adminService.getOne(new QueryWrapper<Admin>().eq("userName", admin.getUserName()));
        if(resultAdmin!=null){
            return R.error("用户名已注册！");
        }
        Admin resultEmail = adminService.getOne(new QueryWrapper<Admin>().eq("email", admin.getEmail()));
        if(resultEmail!=null){
            return R.error("邮箱已注册！");
        }
        adminService.save(admin);
        return R.ok();
    }


    /**
     * 用户注册
     */
    @PostMapping("/adminFormatPssWord")
    public R adminFormatPssWord(@RequestBody Admin admin) {
        Admin resultAdmin = adminService.getOne(new QueryWrapper<Admin>().eq("email", admin.getEmail()));
        if(resultAdmin==null){
            return R.error("邮箱不存在,请重新输入");
        }
        adminService.update(new UpdateWrapper<Admin>().set("password","123")
                .eq("email",admin.getEmail()));
        return R.ok();
    }

    /**
     * 根据条件分页查询用户信息
     * @param pageBean
     * @return
     */
    @RequestMapping("/admin/list")
    public R list(@RequestBody PageBean pageBean){
        System.out.println(pageBean);
        String query=pageBean.getQuery().trim();
        Page<Admin> page=new Page<>(pageBean.getPageNum(),pageBean.getPageSize());
        Page<Admin> pageResult = adminService.page(page, new QueryWrapper<Admin>().like(StringUtil.isNotEmpty(query),"userName", query));
        Map<String,Object> map=new HashMap<>();
        map.put("adminList",pageResult.getRecords());
        map.put("total",pageResult.getTotal());
        return R.ok(map);
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @GetMapping("/admin/delete/{id}")
    public R delete(@PathVariable(value = "id") Integer id){
        adminService.removeById(id);
        return R.ok();
    }



}
